package com.ruibang.glass.portal.components;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.DictEntityInfo;
import com.ruibang.glass.portal.entity.*;
import com.ruibang.glass.portal.service.*;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;


/***
 * 加载缓存数据
 */
@Component
public class InitCacheData implements CommandLineRunner {

    @Autowired
    private UserService userService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private NewMenuService menuService;

    @Autowired
    private RedisSingleService redisService;

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private DictService dictService;

    @Autowired
    private SysProvinceService sysProvinceService;

    @Autowired
    private SysCityService sysCityService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductLineService productLineService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private WorkshopService workshopService;

    @Override
    public void run(String... args) throws Exception {
        List<String> userIds = userService.list().stream().map(UserEntity::getUserId).collect(toList());
        if (CollectionUtils.isNotEmpty(userIds)) {
            redisService.set(RedisKey.CHECK_LOGIN_PARAM_INFO, JSON.toJSONString(userIds));
        }


        //设置用户，部门，角色，菜单Map集合到redis
        //部门
        List<DeptEntity> deptEntities = deptService.list();
        if (CollectionUtils.isNotEmpty(deptEntities)) {
            deptEntities.forEach(dept -> {
                if (dept != null && StringUtils.isNotBlank(dept.getDeptName())) {
                    redisService.hset(RedisKey.DEPT_GROUP_TYPE, dept.getDeptId(), dept.getDeptName());
                }
            });
        }

        //用户
        List<UserEntity> userEntities = userService.list();
        if (CollectionUtils.isNotEmpty(userEntities)) {
            userEntities.forEach(user -> {
                if (user != null && StringUtils.isNotBlank(user.getName())) {
                    redisService.hset(RedisKey.USER_GROUP_TYPE, user.getUserId(), user.getName());
                    redisService.hset(RedisKey.LOGIN_NAME_GROUP_TYPE, user.getUserId(), user.getLoginName());
                    redisService.hset(RedisKey.USER_GROUP_DEPT_TYPE, user.getUserId(), user.getDeptId());
                    redisService.hset(RedisKey.USER_GROUP_DEPT_NAME_TYPE, user.getUserId(), user.getDeptName());
                }
            });
        }

        //角色
        List<RoleEntity> roleEntities = roleService.list();
        if (CollectionUtils.isNotEmpty(roleEntities)) {
            roleEntities.forEach(role -> {
                if (role != null && StringUtils.isNotBlank(role.getRoleName())) {
                    redisService.hset(RedisKey.ROLE_GROUP_TYPE, role.getRoleId(), role.getRoleName());
                }
            });
        }

        //菜单
        List<NewMenu> newMenus = menuService.list();
        if (CollectionUtils.isNotEmpty(newMenus)) {
            newMenus.forEach(menu -> {
                if (menu != null && StringUtils.isNotBlank(menu.getName())) {
                    redisService.hset(RedisKey.MENU_GROUP_TYPE, menu.getMenuId(), menu.getName());
                }
            });
        }

        //字典
        List<DictEntity> dictEntities = dictService.list();
        if (CollectionUtils.isNotEmpty(dictEntities)) {
            dictEntities.forEach(dict -> {
                if (dict != null && StringUtils.isNotBlank(dict.getValue())) {
                    redisService.hset(RedisKey.DICT_GROUP_TYPE, dict.getCode(), dict.getValue());
                }
            });
        }

        if (CollectionUtils.isNotEmpty(dictEntities)) {
            List<DictEntityInfo> list = new ArrayList<>();
            dictEntities.forEach(dict -> {
                DictEntityInfo dictEntityInfo = new DictEntityInfo();
                dictEntityInfo.setGroupType(dict.getGroupType());
                dictEntityInfo.setValue(dict.getValue());
                dictEntityInfo.setName(dict.getName());
                list.add(dictEntityInfo);
            });
            redisService.set(RedisKey.DICT_NEW_GROUP_TYPE, JSON.toJSONString(list));
        }
        List<SysProvince> provinceList = sysProvinceService.list();
        if (CollectionUtils.isNotEmpty(provinceList)) {
            provinceList.forEach(sysProvince -> {
                if (sysProvince != null && StringUtils.isNotBlank(sysProvince.getName())) {
                    redisService.hset(RedisKey.DICT_GROUP_TYPE, sysProvince.getId().toString(), sysProvince.getName());
                }
            });
        }
        List<SysCity> sysCities = sysCityService.list();
        Map<Integer, List<SysCity>> collect = sysCities.stream().collect(Collectors.groupingBy(SysCity::getProvinceId));
        for (Integer provinceId : collect.keySet()) {
            redisService.hset(RedisKey.DICT_GROUP_TYPE, provinceId.toString(), JSON.toJSONString(collect.get(provinceId)));
        }

        //产品管理
        List<Product> products = productService.list();
        if (CollectionUtils.isNotEmpty(products)) {
            products.forEach(product -> {
                if (product != null && StringUtils.isNotBlank(product.getProductCode())) {
                    redisService.hset(RedisKey.PRODUCT_MANAGEMENT_GROUP_TYPE, product.getProductCode(), product.getProductName());
                }
            });
        }

        //产线
        List<ProductLine> productLineList = productLineService.list();
        if (CollectionUtils.isNotEmpty(productLineList)) {
            Map<String, Serializable> mapData = productLineList.stream().collect(Collectors.toMap(ProductLine::getProductLineId, ProductLine::getProductLineName));
            redisService.hmset(RedisKey.PRODUCT_LINE_GROUP_TYPE, mapData);
        }

        //工序
        List<Area> areaList = areaService.list();
        if (CollectionUtils.isNotEmpty(areaList)) {
            Map<String, Serializable> mapData = areaList.stream().collect(Collectors.toMap(Area::getAreaId, Area::getAreaName));
            redisService.hmset(RedisKey.AREA_GROUP_TYPE, mapData);
        }

        List<Workshop> workshops = workshopService.list();
        if (CollectionUtils.isNotEmpty(workshops)) {
            Map<String, Serializable> mapData = workshops.stream().collect(Collectors.toMap(Workshop::getWorkshopId, Workshop::getWorkshopName));
            redisService.hmset(RedisKey.WORKSHOP_GROUP_TYPE, mapData);
        }
    }
}
